﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class BidProductUpdate : BasePage
    {
        public BidProductUpdate()
        {
            //设置页面 模块以及操作类型
            this.ModuleNo = Module.采购产品管理.ToString();
            this.PageOperateType = OperateType.编辑.ToString();
        }


        protected HZ.DrugTrade.BLL.BLL_Common bll = new HZ.DrugTrade.BLL.BLL_Common();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定选择的产品信息
                if (Request["goodsid"] != null)
                {
                    string goodsId = Request.QueryString["goodsid"];

                    IBLL_Goods ig = Factory.BLL_Goods();
                    Model.View.Goods goodsModel = ig.GetGoodsByID(goodsId);
                    if (goodsModel != null)
                    {
                        lblGoodsID.Text = goodsId;

                        txtGoodsName.Value = goodsModel.GoodsName;
                        //txtShortName.Value = goodsModel.ShortName;

                        //this.ddlSortSource.SelectedValue = goodsModel.Source.ToString();
                        txtOutlookc.Text = goodsModel.Outlookc;
                        txtMaterial.Text = goodsModel.Material;
                        txtUnit.Text = goodsModel.Unit;
                        txtPackUnit.Text = goodsModel.PackUnit;

                        txtCompany_SC.Value = goodsModel.CompanyName_SC;
                        txtCompany_TB.Value = goodsModel.CompanyName_TB;
                        hfdSortSource.Value = goodsModel.Source.Value.ToString();//产品来源
                        hfdGoodsId.Value = goodsId;
                    }
                    else
                    {
                        PageError("产品不存在！", "");
                    }
                }


                #region 绑定所有项目
                //IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
                //ProcurementProject procurementProject = new ProcurementProject();
                //procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                //procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                //procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                //procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                //int temp;
                //IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                //ddlProject.DataSource = projectList;
                //ddlProject.DataTextField = "ProjectName";
                //ddlProject.DataValueField = "ProjectID";
                //ddlProject.DataBind();
                ////绑定从前一页面传过来的project
                //if (!string.IsNullOrEmpty(Request.QueryString["projectID"]))
                //{
                //    for (int i = 0; i < ddlProject.Items.Count; i++)
                //    {
                //        if (ddlProject.Items[i].Value == Request.QueryString["projectID"])
                //        {
                //            ddlProject.Items[i].Selected = true;
                //        }
                //    }
                //}
#endregion 
            }
        }

        /// <summary>
        /// 根据产品的来源属性计算出其采购限价
        /// 对于非中标药采购限价计算规则
        ///  >575 -75
        ///  <=575  /1.15
        ///
        /// </summary>
        /// <param name="goodsModel"></param>
        /// <returns></returns>
        private string GetProcurePrice(HZ.DrugTrade.Model.View.Goods goodsModel)
        {
            if (goodsModel.Source <= 1)
            {
                return goodsModel.BidPrice.Value.ToString("F");
            }
            else
            {
                if (goodsModel.LimitPrice != null)
                {
                    if (goodsModel.LimitPrice.Value <= 575)
                    {
                        return (goodsModel.LimitPrice.Value / (decimal)1.15).ToString("F");
                    }
                    else
                    {
                        return (goodsModel.LimitPrice.Value - 75).ToString("F");
                    }
                }
                else
                {
                    return "0.00";
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="goodsModel"></param>
        /// <returns></returns>
        private decimal GetLimitPrice(HZ.DrugTrade.Model.Goods goodsModel)
        {

            if (goodsModel.BidPrice != null)
            {
                if (goodsModel.BidPrice.Value <= 500)
                {
                    return goodsModel.BidPrice.Value * (decimal)1.15;
                }
                else
                {
                    return goodsModel.BidPrice.Value + 75;
                }
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 编辑采购产品
        /// </summary>
        private void AddBidProduct()
        {
          
            //获取新增采购产品的相关信息
            Goods goods = new Goods();

            if (!string.IsNullOrEmpty(hfdGoodsId.Value))
            {
                goods.GoodsID = hfdGoodsId.Value.ToString();
                goods.GoodsName = this.txtGoodsName.Value;
                goods.Material = this.txtMaterial.Text;
                goods.Outlookc = this.txtOutlookc.Text;
                goods.Unit = this.txtUnit.Text;
                goods.PackUnit = this.txtPackUnit.Text;
                //goods.Source = Convert.ToInt16(this.ddlSortSource.SelectedValue);
                //使用方法，执行操作，采购产品
                IBLL_Goods ipb = Factory.BLL_Goods();
                int flag = ipb.UpdateGoods(goods,CurrentAccount.UserID);
                if (flag == 1)
                {
                    AddProcurecatalog();//编辑采购目录 edit by zdc 升级后view_procurecatalog_tb
                    PageInfo("操作成功！", "BidProductList.aspx");
                }
                else if (flag == 0)
                {
                    PageError("操作失败！", this.ReturnUrl);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("未选择任何产品！", this);
            }
        }
        /// <summary>
        /// 编辑采购目录 edit by zdc 升级后view_procurecatalog_tb
        /// </summary>
        private void AddProcurecatalog()
        {

            //获取新增采购产品的相关信息
            View_Procurecatalog_TB vpt = new View_Procurecatalog_TB();

            if (!string.IsNullOrEmpty(hfdGoodsId.Value))
            {
                vpt.GoodsID = hfdGoodsId.Value.ToString();

                //vpt.IsJiCeng = Convert.ToInt32(ddlIsBasicRemedy.SelectedValue);//0允许 1不允许 add by zdc
                vpt.GoodsName = this.txtGoodsName.Value;
                //goods.ShortName = this.txtShortName.Value;
                vpt.ProcureStartTime = DateTime.Parse("2011-1-1 10:06:09");
                vpt.ProcureEndTime = DateTime.Parse("2012-12-31 10:06:09");
                vpt.Material = this.txtMaterial.Text;
                vpt.Outlookc = this.txtOutlookc.Text;
                vpt.Unit = this.txtUnit.Text;
                vpt.PackUnit = this.txtPackUnit.Text;
                //vpt.SortSource = Convert.ToInt16(this.ddlSortSource.SelectedValue);

                //使用方法，执行操作，采购产品
                IBLL_Goods ipb = Factory.BLL_Goods();
                int flag = ipb.UpdateVPT(vpt, CurrentAccount.UserID);
                if (flag == 1)
                {
                    PageInfo("操作成功！", "BidProductList.aspx");
                }
                else if (flag == 0)
                {
                    PageError("操作失败！", this.ReturnUrl);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("未选择任何产品！", this);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                AddBidProduct();
                
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            //使用方法，执行操作，采购产品
            IBLL_Goods ipb = Factory.BLL_Goods();

            int flag = ipb.DelGoods(this.lblGoodsID.Text, CurrentAccount.UserID);

            if (flag == 1)
            {
                PageInfo("操作成功！", "BidProductList.aspx");
            }
            else if (flag == 0)
            {
                PageError("操作失败！", this.ReturnUrl);
            }
        }
    }
}
